<template>
  <div class="role-limits">
    <div class="role-header">
      <div class="addmemo">
        <el-input
          placeholder="请输入角色名称"
          v-model="searchName"
          class="input-with-select"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            class="search"
            @click="searchData"
          ></el-button>
        </el-input>
        <el-button
          type="success"
          icon="el-icon-plus"
          class="add-btn"
          size="medium"
          @click="open"
          >增加</el-button
        >
      </div>
    </div>
    <el-table
      :data="rowData"
      style="width: 100%"
      row-key="id"
      :row-style="{ height: '48px' }"
    >
      <el-table-column
        :prop="item.name"
        v-for="(item, index) in clumnData"
        :key="index"
        :label="item.lable"
        :width="item.width"
      ></el-table-column>
      <el-table-column label="操作" min-width="300px">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-s-tools"
            @click="selectLimits(scope.row)"
            >设定</el-button
          >
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="selectRole(scope.row)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeRole(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pageFtoor :rowData="rowData" :countNum="countNum"></pageFtoor>
    <rolePopUp ref="rolePopUp" @addTable="addTable"></rolePopUp>
  </div>
</template>
<script>
import { role, addrole, seletrole, delrole } from "../../utils/api/role";
import storage from "../../utils/storage";
import rolePopUp from "../../components/components_wxq/rolePopUp.vue";
import pageFtoor from "../../components/components_wxq/pageFtoor.vue";

export default {
  name: "roleTable",
  components: {
    rolePopUp,
    pageFtoor,
  },
  data() {
    return {
      countNum: 20,
      nowPage: 1,
      id: null,
      clumnData: [
        { name: "id", lable: "ID", width: 120 },
        { name: "title", lable: "名称", width: 280 },
        { name: "permission", lable: "权限值", width: 200 },
      ],
      rowData: [],
      searchName: "",
    };
  },
  methods: {
    searchData() {
      console.log(this.searchName);
      this.setTable();
    },
    selectLimits(obj) {
      this.$router.push({
        name: "limitsPage",
        params: {
          ...obj,
        },
      });
    },
    selectRole(obj) {
      console.log(obj);
      this.$refs.rolePopUp.visbleUp(obj);
    },
    removeRole(obj) {
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delrole(obj).then((data) => {
            console.log(data);
            if (data.data.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.setTable();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    open() {
      this.$refs.rolePopUp.visbleUp();
    },
    addTable(obj) {
      console.log(obj);
      if (obj.id) {
        seletrole(obj).then((data) => {
          console.log(data);
          this.setTable();
        });
      } else {
        addrole(obj).then((data) => {
          console.log(data);
          if (data.data.code == 200) {
            this.setTable();
          }
        });
      }
    },
    setTable() {
      role(this.searchName).then((data) => {
        console.log(data);
        this.rowData = data.data.data.role;
        this.countNum = data.data.data.count;
      });
    },
    changePage(num) {
      console.log(num);
      this.nowPage = num;
      this.setTable();
    },
  },
  computed: {},
  watch: {},
  created() {
    this.id = storage.getStorage("id");
    console.log(storage.getStorage("id"));
    this.setTable();
  },
};
</script>
<style lang="less" scoped>
.role-limits {
  padding: 20px 30px;
  background-color: #ffffff;
  border-top: 4px solid #4091ff;
  border-bottom: 2px solid #ccc;
  border-radius: 8px 8px 0 0;
}
.role-header {
  width: 100%;
  height: 80px;
  display: flex;
  align-items: center;
  //   padding: 0 32px;
  box-sizing: border-box;
}
.addmemo {
  display: flex;
  width: 100%;
  justify-content: space-between;

  .el-input {
    width: 40%;
    margin-right: 32px;
  }
  .el-input /deep/ .el-input__inner {
    &:focus {
      border: 1px solid #4091ff;
    }
  }
  .search {
    background-color: #4091ff;
    color: #fff;
    border-radius: 0 4px 4px 0;
    border: 1px solid #4091ff;
  }
  .add-btn {
    padding: 0 12px !important;
    height: 36px;
  }
}
.el-table {
  .el-button {
    padding: 6px 8px;
  }
}
</style>
